package com.yitongmao.core.glide;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.module.GlideModule;
import com.yitongmao.common.FileUtils;

import java.io.File;

public class MyGlideModule implements GlideModule {
    public static final int defaultCacheSize = 1024 * 1024 * 500;//默认500M本地缓存

    @Override
    public void applyOptions(final Context context, GlideBuilder builder) {
        builder.setDiskCache(new DiskLruCacheFactory(new DiskLruCacheFactory.CacheDirectoryGetter() {
            @Override
            public File getCacheDirectory() {
                return glideCacheFilePath(context);
            }
        }, defaultCacheSize));
        // builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }

    private File glideCacheFilePath(Context context) {
        File file = null;
        if (FileUtils.ExistSDCard()){
            file = new File(FileUtils.ImageCacheSD + "/glidecache/");
        }else {
            file = new File(FileUtils.getFilesDirPhone(context) + "/glidecache/");
        }
        if (file.isDirectory()) {
            return file;
        } else {
            file.mkdirs();
        }

        return file;
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
//        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(GlideProgressListener.getGlideOkHttpClient()));
    }
}
